<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/10
 * Time: 15:50
 * 描述：用户（推广者）的业务信息，加pid,建立推广者等
 */

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\PromoterModel;

class PromoterController extends Controller
{
    public $rules = [
        'username' => 'required|max:10',
        'password' => 'required|max:10',
        'groupid' => 'required',
        'grouptitle' => 'required',
        'groupname' => 'required',
        'pid' => 'required',
        'groupqq' => 'required',

    ];
    public $message = [
        'username.required'=>'账号不能为空！',
        'username.max'=>'账号不能大于10位！',
        'password.required'=>'密码不能为空！',
            // 'code.required'=>'验证码不能为空！',
            // 'code.between'=>'验证码必须为4位！',
        'groupid.required'=>'groupid不能为空！',
        'grouptitle.required'=>'grouptitle不能为空！',
        'groupname.required'=>'groupname不能为空！',
        'pid.required'=>'pid不能为空！',
        'groupqq.required'=>'groupqq不能为空！',

    ];
    public function getList(Request $request)
    {
        $promoterM = new PromoterModel();
        $promoters = $promoterM->getList();
        //dd($promoters);
        return view("/admins/promoter/getlist",['res'=>$promoters]);
    }
    public function getJsonList(Request $request)
    {
        $promoterM = new PromoterModel();
        $promoters = $promoterM->getList();
        //返回值有待完善
        $res['code']=0;
        $res['msg']="";
        $res['count']=10;
        $res['data'] = $promoters->toArray()['data'];
        return json_encode($res);
    }
    public function addPromoterForm(Request $request)
    {//添加一个推广者信息
        //var_dump($request->session());
        return view("/admins/promoter/addpromoterform");
    }
    public function addPromoter(Request $request)
    {//添加一个推广者信息
        //dd($request->all());

        $this->validate($request,$this->rules,$this->message);
        //$postData = $request->all();

        $data['userName'] = $request->input('username');
        $data['passWord'] = $request->input('password');
        $data['groupId'] = $request->input('groupid');
        $data['groupTitle'] = $request->input('grouptitle');
        $data['groupName'] = $request->input('groupname');
        $data['PID'] = $request->input('pid');
        $data['groupQQ'] = $request->input('groupqq');
        $data['isAction'] = 0;
        if($request->input('isaction')=="on")
        {
            $data['isAction'] = 1;
        }

        $data['joinTime'] = date("Y-m-d h:i:s");

        $Promoter = new \App\Models\PromoterModel();
        $result = $Promoter->saveInfo($data);

        if($result)
        {
            $msg = [
                'message'=>'添加成功！',
                'url' =>url()->previous(),
                'jumpTime'=>5,
            ];
            return view('/comm/msginfojump')->with($msg);

        }
        else{
            $msg = [
                'message'=>'添加失败！',
                'url' =>url()->previous(),
                'jumpTime'=>5,
            ];
            return view('/comm/msginfojump')->with($msg);
        }



    }
    public function editPromoterForm(Request $request)
    {
        //dd($request->all());
        $id = $request->input('id');
        if($id == '' || $id ==null )
        {
            exit('操作有错误');

        }
        $PromoterModel = new PromoterModel();
        $res = $PromoterModel->getPromoter($id);
        if($res)
        {//以后再优化逻辑

        }else{
            exit('操作有错误');
        }

        return view("/admins/promoter/editpromoterform",['res'=>$res]);

    }
    public function editPromoter(Request $request)
    {//编辑一个推广者信息
        //dd($request->all());
        $this->validate($request,$this->rules,$this->message);
        //$postData = $request->all();
        $data['id'] = $request->input('id');
        $data['userName'] = $request->input('username');
        $data['passWord'] = $request->input('password');
        $data['groupId'] = $request->input('groupid');
        $data['groupTitle'] = $request->input('grouptitle');
        $data['groupName'] = $request->input('groupname');
        $data['PID'] = $request->input('pid');
        $data['groupQQ'] = $request->input('groupqq');
        $data['isAction'] = 0;
        if($request->input('isaction')=="on")
        {
            $data['isAction'] = 1;
        }

        //dd($data);
        $Promoter = new \App\Models\PromoterModel();
        $result = $Promoter->updataInfo($data);

        if($result)
        {
            $msg = [
                'message'=>'你已经修改成功！',
                'url' =>url()->previous(),
                'jumpTime'=>5,
            ];
            return view('/comm/msginfojump')->with($msg);

        }
        else{
            $msg = [
                'message'=>'你已经修改失败！',
                'url' =>url()->previous(),
                'jumpTime'=>5,
            ];
            return view('/comm/msginfojump')->with($msg);
        }
    }
    public function delPromoter(Request $request)
    {//删除一个推广者信息
        $id = $request->input('id');
        if($id == '' || $id ==null )
        {
            exit('操作有错误');

        }
        $Promoter = new \App\Models\PromoterModel();
        $result = $Promoter->delInfo($id);

        if($result)
        {
            $msg = [
                'message'=>'你已经删除成功！',
                'url' =>url()->previous(),
                'jumpTime'=>5,
            ];
            return view('/comm/msginfojump')->with($msg);

        }
        else{
            $msg = [
                'message'=>'你删除失败！',
                'url' =>url()->previous(),
                'jumpTime'=>5,
            ];
            return view('/comm/msginfojump')->with($msg);
        }
    }




}